﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace LittleBirdAuction.Core
{
    public class DiabloItem
    {
        public string name { get; set; }
        public uint id { get; set; }
        public string hash { get; set; }
        public ulong ilevel { get; set; }

        public string heading { get; set; }
        public RarityId rarity { get; set; }
        public string type { get; set; }

        public ulong dpsarmor { get; set; }
        public ulong block_chance { get; set; }
        public ulong block_min { get; set; }
        public ulong block_max { get; set; }
        public ulong damage_aps { get; set; }
        public ulong damage_min { get; set; }
        public ulong damage_max { get; set; }

        public ulong bid_start { get; set; }
        public ulong bid_current { get; set; }
        public ulong bid_max { get; set; }
        public ulong buyout { get; set; }

        public uint time_remaining { get; set; }
        public uint time_expiring { get; set; }

        public List<Stat> stats = new List<Stat>(AuctionConst.ITEM_STAT_LIMIT);
        public List<Stat> sockets = new List<Stat>(AuctionConst.ITEM_SOCKET_LIMIT);

        public void Empty()
        {
            name = string.Empty;
            id = 0;
            hash = string.Empty;
            ilevel = 0;

            rarity = RarityId.RARITY_UNKNOWN;
            heading = string.Empty;
            type = string.Empty;

            dpsarmor = 0;
            block_chance = 0;
            block_min = 0;
            block_max = 0;
            damage_aps = 0;
            damage_min = 0;
            damage_max = 0;

            bid_start = 0;
            bid_current = 0;
            bid_max = 0;
            buyout = 0;

            time_remaining = 0;
            time_expiring = 0;

            stats.Clear();
            sockets.Clear();
        }

        public bool IsValid()
        {
            return !string.IsNullOrEmpty(type);
        }

         /**/
        public bool FindStat( string pattern, bool substring , out Stat best_stat )
        {
            int best_score = 0;

            best_stat = new Stat();

            // fail on empty
            if(string.IsNullOrEmpty(pattern) || stats.Count ==0)
            {
                return false;
            }

            // search stats for object with matching pattern
            foreach( Stat stat in stats )
            {
                // substring search
                if(!string.IsNullOrEmpty(Tools.StrSearch(pattern, stat.text, !substring)))
                {
                    int score = stat.text.Length - pattern.Length;

                    // elect best match
                    if(score < best_score)
                    {
                        best_score = score;
                        best_stat = stat;
                    }
                }
            }

            return true;
        }
    }

    public class Stat
    {
        [DefaultValue("")]
        public string text { get; set; }

        public List<long> values = new List<long>(AuctionConst.ITEM_STAT_VALUE_LIMIT);

        [DefaultValue(GemTypeId.GEM_TYPE_EMPTY)]
        public GemTypeId gem_type { get; set; }

        [DefaultValue(0)]
        public uint gem_rank { get; set; }


        public static Stat GetDefault()
        {
            Stat s = new Stat();
            return s;
        }
    }
}
